package com.six.firstpage.controller.api;

import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.six.core.entity.Notice;
import com.six.core.util.JwtUtil;
import com.six.core.util.WebUtil;
import com.six.core.vo.Result;
import com.six.firstpage.service.NoticeService;
import com.six.firstpage.vo.NoticeVo;
import com.six.firstpage.vo.SearchTime;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author:zzh
 * @create: 2023-02-27 15:15
 * @Description:
 */
@Api(value = "firstpage/api/notice", tags = {"公告管理前端API"})
@RestController
@RequestMapping("firstpage/api/notice")
//@CrossOrigin
public class ApiNoticeController {
    @Autowired
    private NoticeService noticeService;

    @GetMapping("getNotice")
    public Result getNotice(){
        List<Notice> allNotice = noticeService.getAllNotice();
        return new Result(2000,"查询公告成功",allNotice);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "SearchTime", name = "searchTime", value = "", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "current", value = "", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageSize", value = "", required = false)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/getNoticeByTime/{current}/{pageSize}")
    public Result getNoticeByTime(@RequestBody NoticeVo noticeVo, @PathVariable Integer current, @PathVariable  Integer pageSize){

        IPage<NoticeVo> notice = noticeService.getNoticeByTime(noticeVo, current, pageSize);
        return new Result(2000,"查询公告成功",notice);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Notice", name = "notice", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("addNotice")
    public Result addNotic(@RequestBody Notice notice){
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> stringObjectMap = JwtUtil.decodeJWT(token);
        Integer empId =(Integer) stringObjectMap.get("empId");
        notice.setEmpid(empId);
        Integer integer = noticeService.addNotice(notice);
        if (integer>0){
            return new Result(2000,"添加公告成功");
        }
        return new Result(5000,"添加公告失败");

    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Notice", name = "notice", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "PUT")
    @PutMapping("updateNotice")
    public Result updateNotic(@RequestBody Notice notice){
        Integer integer = noticeService.updateNotice(notice);
        if (integer>0){
            return new Result(2000,"修改公告成功");
        }
        return new Result(5000,"修改公告失败");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "GET")
    @GetMapping("delNotice/{id}")
    public Result delNotice(@PathVariable Integer id){
        Integer integer = noticeService.delNotice(id);
        if (integer>0){
            return new Result(2000,"删除公告成功");
        }
        return new Result(5000,"删除公告失败");
    }
}
